package com.enlorenz.common.geo.gts.baidu;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

/**
 * 获取区域代号 根据点的市和区获取
 * @author jueyue
 * 
 */
public class GtsCodeData {

	private static GtsCodeData instance;

	private GtsCodeData() {
		getCodeMap();
	}

	public static GtsCodeData getInstance() {
		if (instance == null) {
			instance = new GtsCodeData();
		}
		return instance;
	}

	/**
	 * key 市名+取名 value 区域代号 ex:长沙市|芙蓉区 --010121
	 */
	public Map<String, String> codeMap = new HashMap<String, String>();

	/**
	 * 获取区域代号
	 */
	private void getCodeMap() {
		try {
			getData(new File("c:\\codeData.xls"), 0);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void getData(File file, int ignoreRows)
			throws FileNotFoundException, IOException {
		int rowSize = 0;
		BufferedInputStream in = new BufferedInputStream(new FileInputStream(
				file));
		POIFSFileSystem fs = new POIFSFileSystem(in);
		HSSFWorkbook wb = new HSSFWorkbook(fs);
		HSSFCell cell = null;
		for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets(); sheetIndex++) {
			HSSFSheet st = wb.getSheetAt(sheetIndex);
			for (int rowIndex = ignoreRows; rowIndex <= st.getLastRowNum(); rowIndex++) {
				HSSFRow row = st.getRow(rowIndex);
				if (row == null) {
					continue;
				}
				int tempRowSize = row.getLastCellNum() + 1;
				if (tempRowSize > rowSize) {
					rowSize = tempRowSize;
				}
				String[] values = new String[rowSize];
				Arrays.fill(values, "");
				boolean hasValue = false;
				for (int columnIndex = 0; columnIndex <= 2; columnIndex++) {
					String value = "";
					cell = row.getCell(columnIndex);
					if (cell != null) {
						  switch (cell.getCellType()) {
		                     case HSSFCell.CELL_TYPE_STRING:
		                         value = cell.getStringCellValue();
		                         break;
		                     case HSSFCell.CELL_TYPE_NUMERIC:
		                    	value = new DecimalFormat("0").format(cell
		                                   .getNumericCellValue());
						  }
						hasValue = true;
						values[columnIndex] = value;
					}
				}
				if(hasValue){
					codeMap.put(values[1]+"|"+values[2], values[0]);
				}
			}
		}
		in.close();
	}

	// 用来获得到WEB-INF的路径
	private String getAddress() {
		Class<GtsCodeData> theClass = GtsCodeData.class;
		java.net.URL u = theClass.getResource("");
		String str = u.toString();
		str = str.substring(6, str.length());
		str = str.replaceAll("%20", " ");
		int num = str.indexOf("WEB-INF");
		str = str.substring(0, num + "WEB-INF".length());
		return str;
	}
}
